package zmee.pur.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.property.AmountProp;
import kd.bos.form.IFormView;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.field.AmountEdit;
import kd.bos.form.operate.FormOperate;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.scm.common.util.SrmCommonUtil;

import java.util.*;

/**
 * @Author: xw
 * @Date: 2022/12/03/16:40
 * @Description: 开标后录入金额
 */
public class PurListQuoteBillPlugin extends AbstractBillPlugIn {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        IFormView parentView = this.getView().getParentView();
        if (parentView != null) {
            DynamicObject dataEntity = parentView.getModel().getDataEntity();
            String name = dataEntity.getDataEntityType().getName();
            //当前节点为资信评审
            if ("src_aptitudeaudit2".equals(name)) {
                FormOperate formOperate = (FormOperate) args.getSource();
                String operateKey = formOperate.getOperateKey();
                if ("quote".equals(operateKey)) {
                    //开标状态
                    String openStatus = dataEntity.getString("openstatus");
                    if (!"4".equals(openStatus)) {
                        this.getView().showMessage("请先开标再进行报价！");
                        args.setCancel(true);
                    }
                    long id = dataEntity.getLong("id");
                    DynamicObject aptitudeResult = BusinessDataServiceHelper.loadSingle(id, "src_aptituderesult2");
                    DynamicObjectCollection entryEntity = aptitudeResult.getDynamicObjectCollection("entryentity");
                    int count = (int) entryEntity.stream().filter(item -> "D".equals(item.getString("scoretask.bizstatus"))).count();
                    if (count != entryEntity.size()) {
                        this.getView().showMessage("资质后审结果【资审状态】都要是已评分后，再进行报价！");
                        args.setCancel(true);
                    }
                }
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs args) {
        IFormView parentView = this.getView().getParentView();
        if (parentView != null) {
            DynamicObject dataEntity = parentView.getModel().getDataEntity();
            String name = dataEntity.getDataEntityType().getName();
            //当前节点为资信评审
            if ("src_aptitudeaudit2".equals(name)) {
                FormOperate formOperate = (FormOperate) args.getSource();
                String operateKey = formOperate.getOperateKey();
                if ("quote".equals(operateKey)) {
                    Object supplier = this.getModel().getValue("supplier", 0);
                    if (supplier != null) {
                        return;
                    }
                    long projectId = dataEntity.getLong("id");
                    DynamicObjectCollection entryEntity = this.getModel().getDataEntity(true).getDynamicObjectCollection("entryentity");
                    int[] rowIndex = new int[entryEntity.size()];
                    for (int i = 0; i < entryEntity.size(); i++) {
                        rowIndex[i] = i;
                    }
                    EntryGrid entryGrid = this.getControl("entryentity");
                    entryGrid.selectRows(rowIndex, 0);
                    List<Long> supplierIds = new ArrayList<>();
                    DynamicObject bidOpenSuppliers = BusinessDataServiceHelper.loadSingle(projectId, "src_aptituderesult2");
                    DynamicObjectCollection entryEntitys = bidOpenSuppliers.getDynamicObjectCollection("entryentity");
                    for (DynamicObject entity : entryEntitys) {
                        String isAptitude = entity.getString("isaptitude");
                        if ("1".equals(isAptitude)) {
                            long supplierId = SrmCommonUtil.getPkValue(entity.getDynamicObject("scoretask.supplier"));
                            supplierIds.add(supplierId);
                        }
                    }
                    for (int i = 0; i < supplierIds.size() - 1; i++) {
                        this.getView().invokeOperation("copyentryrow");
                        entryGrid.selectRows(rowIndex, 0);
                    }
                    int i = 0;
                    for (int k = 0, j = rowIndex.length; k < entryEntity.size(); k++, j--) {
                        if (j <= 0) {
                            i++;
                            j = rowIndex.length;
                        }
                        this.getModel().setValue("supplier", supplierIds.get(i), k);
                    }
                }
            }
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IFormView parentView = this.getView().getParentView();
        if (parentView != null) {
            DynamicObject dataEntity = parentView.getModel().getDataEntity();
            String name = dataEntity.getDataEntityType().getName();
            //当前节点为资信评审
            if ("src_aptitudeaudit2".equals(name)) {
                //隐藏部分字段
                //setEntryPropertiesVisible();
                //锁定分录
                this.getView().setEnable(false, "entryentity");
                //开标状态
                String openStatus = dataEntity.getString("openstatus");
                if (openStatus == null || "".equals(openStatus) || "4".equals(openStatus)) {
                    this.getView().setEnable(true, "entryentity");
                }

                //隐藏增行、删行、复制按钮
                this.getView().setVisible(false, "addentry", "deleteentry", "copyentryrow");
            }
        }
    }

    private void setEntryPropertiesVisible() {
        String[] properties = {"purlistseq", "rank", "sysresult", "preresult", "preorderratio", "result", "orderratio", "cfmqty",
                "precfmqty", "firstrank", "deliverdate", "entryrcvorg", "locprice", "isnew", "locamount", "loctaxamount", "loccurr",
                "loctaxprice", "maxprice", "maxtaxprice", "historyprice", "historytaxprice", "price12", "price13", "bestprice",
                "usdprice"
                , "price14", "price15", "exratetable", "exratedate", "exrate"};
        this.getView().setVisible(false, properties);
    }

    private void setEntryPropertiesEnabled() {
        String[] properties = {"turns", "supplier", "material", "qty", "unit", "materialmodel", "currency", "suppliertype", "ispresent",
                "duedate", "effectdate", "purlistentry_supfj", "purlistentry_fj", "isdiscardbid", "isdiscarded", "rowtype"};
        this.getView().setEnable(false, -1, properties);
    }

    private void setPriceEnabled(String taxType) {
        if ("2".equals(taxType)) {
            this.getView().setEnable(true, -1, "amount");
            this.getView().setEnable(false, -1, "taxamount");
            AmountEdit amountEdit = this.getView().getControl("amount");
            AmountProp amountProp = (AmountProp) amountEdit.getProperty();
            amountProp.setMustInput(true);
            amountEdit.setMustInput(true);
        } else {
            this.getView().setEnable(false, -1, "amount");
            this.getView().setEnable(true, -1, "taxamount");
            AmountEdit taxAmountEdit = this.getView().getControl("taxamount");
            AmountProp taxAmountProp = (AmountProp) taxAmountEdit.getProperty();
            taxAmountProp.setMustInput(true);
            taxAmountEdit.setMustInput(true);
        }
    }
}
